home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / PROGTOOL / GWMALLOC.ZIP;1 / GWMALLOC.TAR / gw_malloc / malloc.info < prev    next >
Encoding:
GNU Info File  |  1993-04-08  |  40.8 KB  |  1,057 lines

  1. This is Info file malloc.info, produced by Makeinfo-1.49 from the input
  2. file malloc.texi.
  3.  
  4.    This file is an introduction to the Malloc library which handles
  5. general memory heap management.
  6.  
  7.    Copyright (C) 1992 by Gray Watson and the Antaire Corporation.
  8.  
  9.    Permission is granted to make and distribute verbatim copies of this
  10. manual provided the copyright notice and this permission notice are
  11. preserved on all copies.
  12.  
  13.    Permission is granted to copy and distribute modified versions of
  14. this manual under the conditions for verbatim copying, provided also
  15. that the section entitled "Copying" are included exactly as in the
  16. original, and provided that the entire resulting derived work is
  17. distributed under the terms of a permission notice identical to this
  18. one.
  19.  
  20.    Permission is granted to copy and distribute translations of this
  21. manual into another language, under the above conditions for modified
  22. versions, except that the section entitled "Copying" may be included in
  23. a translation approved by the author instead of in the original English.
  24.  
  25.  
  26. File: malloc.info,  Node: Top,  Next: Copying,  Prev: (dir),  Up: (dir)
  27.  
  28. Malloc Debug Library
  29. ********************
  30.  
  31.    This file documents the general-usage and the inner-workings of the
  32. memory allocation or "malloc" library it accompanies.
  33.  
  34.    This malloc library has been designed as a drop in replacement for
  35. the system's malloc, realloc, calloc, free and other memory management
  36. routines.  For more information about their capabilities, do a `man 3
  37. malloc' to read the system's manual pages.
  38.  
  39.    What is unique about this library is that it contains a number of
  40. powerful debugging facilities including very comprehensive heap testing
  41. and excellent run-time debugging information.  We have found these
  42. capabilities to be superb development tools.
  43.  
  44.    I can be reached at `<gray.watson@antaire.com>' with any questions
  45. or general comments.
  46.  
  47.    Gray Watson, Antaire Corporation.
  48.  
  49. * Menu:
  50.  
  51. * Copying::             Library copying conditions.
  52. * Allocation Basics::   Basic description of terms and functions.
  53. * Features::            Description of the benefits of the library.
  54. * Usage::               How to run programs with the library.
  55. * Code::                Information on the source and general concerns.
  56. * Plugs::               A couple soapbox comments.
  57.  
  58.  
  59. File: malloc.info,  Node: Copying,  Next: Allocation Basics,  Prev: Top,  Up: Top
  60.  
  61. Library Copying Conditions.
  62. ***************************
  63.  
  64.    This package is covered by the GNU Library Public License.  See the
  65. file `COPYING-LIB' for details.  If you would like to do something with
  66. this package that you feel is reasonable but prohibited by the license,
  67. please contact me to see if we can work it out.
  68.  
  69.    *NOTICE*: this is not the GNU Public License but the *library*
  70. public license.  This license allows you to do more with the library
  71. than the standard public license distributed with most GNU software.
  72. Please read `COPYING-LIB' or contact me for more information.
  73.  
  74.    The rest of this section contains some messages from the Free
  75. Software Foundation.  If you find this stuff offensive or annoying,
  76. remember that you probably did not spend any money to get this library
  77. so feel free to heave it into the bit bucket.
  78.  
  79.    The licenses for most software are designed to take away your
  80. freedom to share and change it.  By contrast, the GNU General Public
  81. Licenses are intended to guarantee your freedom to share and change
  82. free software--to make sure the software is free for all its users.
  83.  
  84.    This license, the Library General Public License, applies to some
  85. specially designated Free Software Foundation software, and to any other
  86. libraries whose authors decide to use it.  You can use it for your
  87. libraries, too.
  88.  
  89.    When we speak of free software, we are referring to freedom, not
  90. price. Our General Public Licenses are designed to make sure that you
  91. have the freedom to distribute copies of free software (and charge for
  92. this service if you wish), that you receive source code or can get it
  93. if you want it, that you can change the software or use pieces of it in
  94. new free programs; and that you know you can do these things.
  95.  
  96.    To protect your rights, we need to make restrictions that forbid
  97. anyone to deny you these rights or to ask you to surrender the rights. 
  98. These restrictions translate to certain responsibilities for you if you
  99. distribute copies of the library, or if you modify it.
  100.  
  101.    For example, if you distribute copies of the library, whether gratis
  102. or for a fee, you must give the recipients all the rights that we gave
  103. you. You must make sure that they, too, receive or can get the source
  104. code. If you link a program with the library, you must provide complete
  105. object files to the recipients so that they can relink them with the
  106. library, after making changes to the library and recompiling it.  And
  107. you must show them these terms so they know their rights.
  108.  
  109.    Our method of protecting your rights has two steps: (1) copyright the
  110. library, and (2) offer you this license which gives you legal permission
  111. to copy, distribute and/or modify the library.
  112.  
  113.    Also, for each distributor's protection, we want to make certain that
  114. everyone understands that there is no warranty for this free library.
  115. If the library is modified by someone else and passed on, we want its
  116. recipients to know that what they have is not the original version, so
  117. that any problems introduced by others will not reflect on the original
  118. authors' reputations.
  119.  
  120.  
  121. File: malloc.info,  Node: Allocation Basics,  Next: Features,  Prev: Copying,  Up: Top
  122.  
  123. Basic Description of Terms and Functions.
  124. *****************************************
  125.  
  126.    This section provides a basic definition of terms used throughout the
  127. manual as well as a brief overview of the basic malloc functions and
  128. examples of their use.  It is quite unnecessary for you to read this
  129. section if you are familiar with using the heap allocation functions.
  130.  
  131. * Menu:
  132.  
  133. * Basic definitions::   For defining general terms and concepts.
  134. * Malloc functions::    Functionality supported by all malloc libs.
  135.  
  136.  
  137. File: malloc.info,  Node: Basic definitions,  Next: Malloc functions,  Up: Allocation Basics
  138.  
  139. For Defining General Terms and Concepts.
  140. ========================================
  141.  
  142.    Any program can be divided into 2 logical parts: text and data. 
  143. Text is the actual program code in machine-readable format and data is
  144. the information that the text operates on when it is executing.  The
  145. data, in turn, can be divided into 3 logical parts according to where
  146. it is stored: "static", "stack", and "heap".
  147.  
  148.    Static data is the information whose storage space is compiled into
  149. the program.
  150.              /* global variables are allocated as static data */
  151.              int numbers[10];
  152.      
  153.              main()
  154.              {
  155.                 ...
  156.              }
  157.    Stack data is data allocated at run-time to hold information used
  158. inside of functions.  This data is managed by the system in the space
  159. called stack space.
  160.  
  161.              void foo()
  162.              {
  163.                 /* this local variable is allocated on the stack */
  164.                 float total;
  165.      
  166.                 ...
  167.              }
  168.    Heap data is also allocated at run-time and provides a programmer
  169. with dynamic memory capabilities.
  170.  
  171.              main()
  172.              {
  173.                 char * string;
  174.                 ...
  175.      
  176.                 /* allocate a string of 10 bytes */
  177.                 string = (char *)malloc(10);
  178.                 ...
  179.                 /* de-allocate the string now that I'm done with it */
  180.                 (void)free(string);
  181.      
  182.                 ...
  183.              }
  184.    It is the heap data that is managed by this library.
  185.  
  186.    Although the above is an example of how to use the malloc and free
  187. commands, it is not a good example of why using the heap for run-time
  188. storage is useful.
  189.  
  190.    Consider this: You write a program that reads a file into memory,
  191. processes it, and displays results.  You would like to handle files with
  192. arbitrary size (from 10 bytes to 1.2 megabytes and more).  One problem,
  193. however, is that the entire file must be in memory at one time to do the
  194. calculations.  You don't want to have to allocate 1.2 megabytes when you
  195. might only be reading in a 10 byte file because it is wasteful of system
  196. resources.  Also, you are worried that your program might have to handle
  197. files of more than 1.2 megabytes.
  198.  
  199.    A solution: first checkout the file's size and then, using the
  200. heap-allocation routines, get enough storage to read the entire file
  201. into memory.  The program will only be using the system resources
  202. necessary for the job and you will be guaranteed that your program can
  203. handle any sized file.
  204.  
  205.  
  206. File: malloc.info,  Node: Malloc functions,  Prev: Basic definitions,  Up: Allocation Basics
  207.  
  208. Functionality Supported by all Malloc Libraries.
  209. ================================================
  210.  
  211.    All malloc libraries support 4 basic memory allocation commands. 
  212. These include "malloc", "calloc", "realloc", and "free".
  213.  
  214. `malloc'
  215.      Usage: `pnt = (type *)malloc(unsigned int size);'
  216.  
  217.      The malloc routine is the basic memory allocation routine.  It
  218.      allocates an area of size bytes.  It will return a pointer to the
  219.      space requested.
  220.  
  221. `calloc'
  222.      Usage: `pnt = (type *)calloc(unsigned int number, unsigned int
  223.      size);'
  224.  
  225.      The calloc routine allocates a certain number of items, each of
  226.      size bytes, and returns a pointer to the space.  It is appropriate
  227.      to pass in a `sizeof(type)' value as the size argument.
  228.  
  229.      Also, calloc nulls the space that it returns, assuring that the
  230.      memory is all zeros.
  231.  
  232. `realloc'
  233.      Usage: `new_pnt = (type *)realloc(void * old_pnt, unsigned int
  234.      new_size);'
  235.  
  236.      The realloc function expands or shrinks the memory allocation in
  237.      old_pnt to new_size number of bytes.  Realloc copies the
  238.      information in old_pnt into the new_pnt space up to new_size bytes
  239.      or until it copies all of the information from old_pnt.
  240.  
  241. `free'
  242.      Usage: `(void)free(void * pnt);'
  243.  
  244.      The free routine releases an allocation returned by malloc,
  245.      calloc, or realloc back to the heap.  This allows other parts of
  246.      the program to re-use memory that is not needed anymore.  It also
  247.      guarantees that the process does not grow too big and swallow a
  248.      large portion of the system resources.
  249.  
  250.    *NOTE*: the returned address from the memory allocation/reallocation
  251. functions should *always* be cast to the appropriate pointer type for
  252. the variable being assigned.
  253.  
  254.    *WARNING*: there is a quite common myth that all of the space that
  255. is returned by malloc libraries has already been cleared.  *Only* the
  256. calloc routine will zero the memory space it returns.
  257.  
  258.  
  259. File: malloc.info,  Node: Features,  Next: Usage,  Prev: Allocation Basics,  Up: Top
  260.  
  261. Description of the Benefits of the Library.
  262. *******************************************
  263.  
  264. * Menu:
  265.  
  266. * Overview::                    General debugging concepts.
  267. * Environment variables::       The variable names and their features.
  268. * malloc_dbg program::          Env variable setting utility.
  269. * RC file::                     Format of the run-time configuration file.
  270. * Debug tokens::                Description of the debugging token flags.
  271. * Argument checking::           Special checking of function arguments.
  272.  
  273.  
  274. File: malloc.info,  Node: Overview,  Next: Environment variables,  Up: Features
  275.  
  276. General Debugging Concepts.
  277. ===========================
  278.  
  279.    The features of this library are controlled by a number of
  280. environmental variables.  They enable the memory debugging features at
  281. runtime to help locate problems, chart memory leaks, provide basic
  282. bounds checking, log statistics, etc.. *Note Environment variables::.
  283.  
  284.    The debugging features that are available can be broken down into a
  285. couple basic classifications:
  286.  
  287. `file and line number information'
  288.      One of the nice things about a good debugger is its ability to
  289.      provide the file and line number of an offending piece of code. 
  290.      This library attempts to give this functionality with the help of
  291.      "cpp", the C preprocessor.  If the file `malloc.h' is included,
  292.      the library can provide file and line information for the warning
  293.      messages and errors it generates.
  294.  
  295. `fence-post (i.e. bounds) checking'
  296.      "Fence-post" memory is the area immediately above or below memory
  297.      allocations.  I have found it all to easy to write code that
  298.      accesses above or below an allocation (especially when dealing
  299.      with arrays or strings).  The library can write special values in
  300.      the areas around every allocation so it will notice when these
  301.      areas have been overwritten.
  302.  
  303.      *NOTE*: The library cannot notice when the program reads from these
  304.      areas, only when it writes values.  Also, fence-post checking will
  305.      increase the amount of memory the program allocates.
  306.  
  307. `heap-constancy verification'
  308.      The administration of the library is reasonably complex.  If any
  309.      of the heap-maintenance information is corrupted, the program will
  310.      either crash or give unpredictable results.
  311.  
  312.      By enabling heap-consistency checking, the library will run
  313.      through its administrative structures to make sure all is in
  314.      order.  This will mean that problems will be caught faster and
  315.      diagnosed better.
  316.  
  317.      The drawback of this is, of course, that the library often takes
  318.      quite a long time to do this.  It is suitable to enable this only
  319.      during development and debugging sessions.
  320.  
  321.      *NOTE*: the heap checking routines cannot guarantee that the tests
  322.      will not cause a segmentation-fault if the heap administration
  323.      structures are properly (or improperly if you will) overwritten. 
  324.      In other words, they will verify that everything is okay but may
  325.      not inform the user of problems in a graceful manner.
  326.  
  327. `logging statistics'
  328.      One of the initial reasons why I personally wanted malloc-debug
  329.      capabilities is to track my programs' memory usage; specifically to
  330.      locate memory "leaks" which are places where allocated memory is
  331.      never getting freed.
  332.  
  333.      The library has a number of logging capabilities that can track
  334.      run-time memory usage, administrative actions, final program
  335.      statistics, as well as un-freed memory pointers.  This information
  336.      is also good at providing more general debugging feedback.
  337.  
  338. `examining unfreed memory'
  339.      Another common problem with programs is that they free a memory
  340.      pointer but then use go on to use it again by mistake.  This can
  341.      lead to mysterious crashes and unexplained problems.
  342.  
  343.      To combat this, the library can write special values into a block
  344.      of memory after it has been freed.  This serves two purposes: it
  345.      will make sure that the program will get garbage data if it trying
  346.      to access the area again, and it will allow the library to verify
  347.      the area later for signs of overwriting.
  348.  
  349.    If any of the above debugging features detect an error, the library
  350. will try to recover.  If logging is enabled then an error will be
  351. logged with as much information as possible.
  352.  
  353.    The error messages that the library displays are designed to give the
  354. most information for developers.  If the error message is not
  355. understood, then it is most likely just trying to indicate that a part
  356. of the heap has been corrupted.  The bug is most likely near the last
  357. call made to the library so reviewing the code around this area is
  358. recommended.
  359.  
  360.    The library can be configured to quit immediately when an error is
  361. detected and to dump a core file or memory-image.  This can be examined
  362. with a debugger to determine the source of the problem.
  363.  
  364.    When running our programs in a debugger such as gdb (the *excellent*
  365. GNU debugger), I always put a break-point in `_malloc_perror()' which
  366. is the internal error routine for the library.  The program will then
  367. hit the break-point as soon as a memory problem is detected.
  368.  
  369.    Other malloc-debug libraries also support the ability to dump core
  370. and then continue running.  I decided not to support this once it was
  371. determined that some versions of `fork' make calls to malloc which
  372. would cause the library to go recursive.
  373.  
  374.    *NOTE*: do not be surprised if the library catches problems with
  375. your system's library routines.  It took me four hours once to finally
  376. come to the conclusion that the localtime call, included in SunOS
  377. release 4.1, was overwriting one of its fence-post markers.
  378.  
  379.  
  380. File: malloc.info,  Node: Environment variables,  Next: malloc_dbg program,  Prev: Overview,  Up: Features
  381.  
  382. Environment Variables - Their Names and Features.
  383. =================================================
  384.  
  385.    "Environment variables" are variables that are part of the user's
  386. working environment and are shared by all the programs.  The below
  387. variables are used by the malloc library to enable or disable the memory
  388. debugging features, at runtime.
  389.  
  390.    They can be set either by hand or with the help of the malloc_dbg
  391. program.  *Note malloc_dbg program::.
  392.  
  393.    To set them by hand, C shell (or tcsh) users need to invoke:
  394.  
  395.              setenv variable value;
  396.    Bourne shell (or bash, ksh) users should use:
  397.  
  398.              variable=value;
  399.              export variable;
  400.  
  401. `MALLOC_DEBUG'
  402.      This env variable should be set to a value in hexadecimal which
  403.      corresponds to a set of functionality tokens.  *Note Debug
  404.      tokens::.  For instance, if the user wanted to enabled logging of
  405.      memory transactions (value `0x008') and wanted to check fence-post
  406.      memory (value `0x400') then `MALLOC_DEBUG' should be set to
  407.      `0x408'.
  408.  
  409.      Don't worry about remembering all the hex values of the tokens, the
  410.      malloc_dbg program automates the setting of this variable
  411.      especially.
  412.  
  413. `MALLOC_LOGFILE'
  414.      Set this variable to a filename so that if `MALLOC_DEBUG' has
  415.      logging enabled, the library can log transactions, administration
  416.      information, and/or errors to the file so memory problems and
  417.      usage can be tracked.
  418.  
  419. `MALLOC_ADDRESS'
  420.      When this env variable is set to a hex address (taken from the
  421.      malloc log-file for instance) malloc will abort when it finds
  422.      itself either allocating or freeing that address.
  423.  
  424.      The address can also have an `:number' argument.  For instance, if
  425.      it was set it to `0x3e45:10', the library will kill itself the 10th
  426.      time it sees address `0x3e45'.
  427.  
  428.      This makes it easier to track down specific addresses not being
  429.      freed.
  430.  
  431. `MALLOC_INTERVAL'
  432.      By setting this env variable to a number X, malloc will only check
  433.      the heap every X times.  This means a number of `MALLOC_DEBUG'
  434.      features can be enabled while still running the program within a
  435.      finite amount of time.
  436.  
  437.      I have found that a setting of `100' works well with reasonably
  438.      memory intensive programs.  This of course means that the library
  439.      will not catch errors exactly when they happen but possibly 100
  440.      library calls later.
  441.  
  442. `MALLOC_START'
  443.      Set this env variable to a number X and malloc will begin checking
  444.      the heap after X times.  This means the intensive debugging can be
  445.      started after a certain point in a program.
  446.  
  447.      `MALLOC_START' also has the format file:line.  For instance, if it
  448.      is set to `malloc_t.c:126' malloc will start checking the heap
  449.      after it sees a malloc call from the `malloc_t.c' file, line number
  450.      126.  If line number is 0 then malloc will start checking the heap
  451.      after it sees a call from anywhere in the `malloc_t.c' file.
  452.  
  453.      This allows the intensive debugging to be started after a certain
  454.      routine or file has been reached in the program.
  455.  
  456.  
  457. File: malloc.info,  Node: malloc_dbg program,  Next: RC file,  Prev: Environment variables,  Up: Features
  458.  
  459. Env Variable Setting Utility.
  460. =============================
  461.  
  462.    The malloc_dbg program is designed to assist in the setting of the
  463. environmental variables, especially `MALLOC_DEBUG'. *Note Environment
  464. variables::.  It is designed to print the shell commands necessary to
  465. make the appropriate changes to the environment. Unfortunately, it
  466. cannot make the changes on its own so the output from malloc_dbg should
  467. be sent through the `eval' shell command which will do the commands.
  468.  
  469.    With shells that have aliasing or macro capabilities: csh, tcsh,
  470. bash, ksh, etc., setting up an alias to malloc_dbg to do the eval call
  471. is recommended.  csh/tcsh users (for example) should put the following
  472. in their `.cshrc' file:
  473.  
  474.              alias malloc 'eval `malloc_dbg \!*`'
  475.    This allows the user to execute `malloc args'.
  476.  
  477.    The most basic usage for the program is `malloc_dbg [-b] tag'.  The
  478. -b flag is for generating Bourne-shell type commands (C-shell type are
  479. the default).  The tag argument should match a line from the user's
  480. run-time configuration file.  *Note RC file::.
  481.  
  482.    Here is a detailed list of the flags that can passed to malloc_dbg:
  483.  
  484. `-a address'
  485.      Set the `MALLOC_ADDRESS' variable with the string address (or
  486.      alternatively address:number).
  487.  
  488. `-b'
  489.      Output Bourne-shell type commands.  (C-shell type output is the
  490.      default).
  491.  
  492. `-c'
  493.      Clear/unset all of the variables not specified with other
  494.      arguments.
  495.  
  496.      *NOTE*: clear will never unset the `MALLOC_DEBUG' variable. Use
  497.      `-d 0' or a tag to `none' to achieve this.
  498.  
  499. `-d bitmask'
  500.      Set the MALLOC_DEBUG to the bitmask value which should be in hex. 
  501.      This is overridden (and unnecessary) if a tag is specified.
  502.  
  503. `-e errno'
  504.      Print the malloc error string that corresponds to errno.
  505.  
  506. `-f filename'
  507.      Use this configuration file instead of the RC file
  508.      `$HOME/.mallocrc'.
  509.  
  510. `-i number'
  511.      Set the `MALLOC_INTERVAL' env variable to number.
  512.  
  513. `-l filename'
  514.      Set the `MALLOC_LOGFILE' env variable to filename.
  515.  
  516. `-s number'
  517.      Set the `MALLOC_START' env variable to number (alternatively
  518.      file:line).
  519.  
  520.    If no arguments are specified, malloc_dbg dumps out the current
  521. settings that you have for the malloc environmental variables.  For
  522. example:
  523.  
  524.              MALLOC_DEBUG == '0x6417' (debug1)
  525.              MALLOC_ADDRESS == '0'
  526.              MALLOC_INTERVAL not set
  527.              MALLOC_LOGFILE == 'malloc'
  528.              MALLOC_START not set
  529.  
  530.  
  531. File: malloc.info,  Node: RC file,  Next: Debug tokens,  Prev: malloc_dbg program,  Up: Features
  532.  
  533. Format of the Run-Time Configuration File.
  534. ==========================================
  535.  
  536.    The name of default "RC file" (or run-time configuration file) is
  537. `$HOME/.mallocrc'.  The `$HOME' environmental variable should be set by
  538. the system to point to your home-directory.
  539.  
  540.    The rc file file should contain lines in the general form of:
  541.  
  542.    `tag token1, token2, ...'
  543.  
  544.    tag is to be matched with the tag argument passed to the malloc_dbg
  545. program, token1, token2, ... are debug capability tokens. *Note
  546. malloc_dbg program:: and *Note Debug tokens::.
  547.  
  548.    A line can be finished with a '\' meaning it continues onto the next
  549. line.  Lines beginning with '#' are treated as comments and are ignored
  550. along with empty lines.
  551.  
  552.    I have the below contents in my `.mallocrc' file:
  553.  
  554.      #
  555.      # Malloc run-time configuration file for our malloc-debug library
  556.      #
  557.      
  558.      # no debugging
  559.      none    none
  560.      
  561.      # basic debugging
  562.      debug1    log-stats, log-non-free, log-perror, log-bad-pnt, check-fence
  563.      
  564.      # more logging and some heap checking
  565.      debug2    log-stats, log-non-free, log-perror, log-trans, log-bad-pnt, \
  566.          check-fence, check-heap, check-lists, error-abort
  567.      
  568.      # good utilities
  569.      debug3    log-stats, log-non-free, log-perror, log-trans, log-bad-pnt, \
  570.          log-admin, check-fence, check-heap, check-lists, realloc-copy, \
  571.          free-blank, error-abort
  572.      
  573.      ...
  574.    With the above file, when I say `eval `malloc_dbg debug1`', I enable
  575. the logging of statistics, the logging of non-freed memory, logging of
  576. errors, logging of bad pointer information, and the checking of
  577. fence-post memory areas.
  578.  
  579.    When I say `eval `malloc_dbg none`', all memory debugging features
  580. are disabled.
  581.  
  582.  
  583. File: malloc.info,  Node: Debug tokens,  Next: Argument Checking,  Prev: RC file,  Up: Features
  584.  
  585. Description of the Debugging Token Flags.
  586. =========================================
  587.  
  588.    The below tokens and their corresponding descriptions are for the
  589. setting of the `MALLOC_DEBUG' environmental variable *Note Environment
  590. variables::.  They should be specified in the user's `.mallocrc' file
  591. *Note RC file::.
  592.  
  593. `none'
  594.      no debugging functionality
  595.  
  596. `log-stats'
  597.      log general statistics when malloc_shutdown is called
  598.  
  599. `log-non-free'
  600.      log non-freed memory pointers when malloc_shutdown is called
  601.  
  602. `log-perror'
  603.      log internal error-messages
  604.  
  605. `log-trans'
  606.      log general memory transactions
  607.  
  608. `log-bad-pnt'
  609.      log information about bad-pointers
  610.  
  611. `log-admin'
  612.      log full administrative information
  613.  
  614. `log-blocks'
  615.      log detailed block information when malloc_heap_map is called
  616.  
  617. `log-unknown'
  618.      like log-non-free but logs unknown non-freed memory pointers
  619.  
  620. `check-fence'
  621.      check fence-post memory areas
  622.  
  623. `check-heap'
  624.      verify heap administrative structure
  625.  
  626. `check-lists'
  627.      examine internal heap linked-lists
  628.  
  629. `check-dblock'
  630.      do detailed checking on small allocations
  631.  
  632. `check-dblock-fence'
  633.      check the fence-post areas of small allocations
  634.  
  635. `check-free'
  636.      check to see if free space has been overwritten
  637.  
  638. `check-funcs'
  639.      check the arguments of some functions (mostly string operations)
  640.      looking for bad pointers
  641.  
  642. `realloc-copy'
  643.      always copy data to a new pointer when realloc
  644.  
  645. `free-blank'
  646.      write special values (non-0) into space when it is freed
  647.  
  648. `error-abort'
  649.      abort the program (and dump core) on errors
  650.  
  651. `alloc-blank'
  652.      write special values (non-0) into space when it is alloced
  653.  
  654. `heap-check-map'
  655.      log a heap-map to the logfile every time the heap is checked
  656.  
  657. `print-perror'
  658.      log any errors and messages to the screen via standard-error
  659.  
  660.  
  661. File: malloc.info,  Node: Argument Checking,  Prev: Debug tokens,  Up: Features
  662.  
  663. Check Certain Function Arguments.
  664. =================================
  665.  
  666.    One potential problem with the library and its multitude of checks
  667. and diagnoses is that they only get performed when a malloc function is
  668. called.  One solution this is to include `malloc.h' and compile your
  669. source code with the `MALLOC_FUNC_CHECK' flag defined and enable the
  670. `check-funcs' token *Note Debug tokens::.
  671.  
  672.              gcc -DMALLOC_FUNC_CHECK file.c
  673.    Once you have compiled your source with FUNC_CHECK enabled, you will
  674. have to recompile with it off to disconnect the library *Note Disabling
  675. the Library::.
  676.  
  677.    When this is defined malloc will override a number of functions and
  678. will insert a routine which knowns how to check its own arguments and
  679. then call the real function.  Malloc can check such functions as bcopy,
  680. index, strcat, and strcasecmp (for the full list see the end of
  681. `malloc.h').
  682.  
  683.    When you call strlen, for instance, malloc will make sure the string
  684. argument's fence-post areas have not been overwritten, its file and line
  685. number locations are good, etc.  With bcopy, malloc will make sure that
  686. the destination string has enough space to store the number of bytes
  687. specified.
  688.  
  689.    For all of the arguments checked, if the pointer is not in the heap
  690. then it is ignored since malloc does know anything about it.
  691.  
  692.    *NOTE*: this is one of the newest parts of the library so problems
  693. may still be lurking.
  694.  
  695.  
  696. File: malloc.info,  Node: Usage,  Next: Code,  Prev: Features,  Up: Top
  697.  
  698. How to Run Programs With the Library.
  699. *************************************
  700.  
  701. * Menu:
  702.  
  703. * Allocation macros::           For providing file and line information.
  704. * Extensions::                  Additional non-standard routines.
  705. * Disabling the Library::       How to compile/link without the library.
  706.  
  707.  
  708. File: malloc.info,  Node: Allocation macros,  Next: Extensions,  Up: Usage
  709.  
  710. For Providing File/Line Debugging Information.
  711. ==============================================
  712.  
  713.    By including `malloc.h' in your C files, your calls to calloc, free,
  714. malloc, or realloc are replaced with calls to _calloc_leap, _free_leap,
  715. _malloc_leap, and _realloc_leap.
  716.  
  717.    These leap macros use the c-preprocessor `__FILE__' and `__LINE__'
  718. macros which get replaced at compilation time with the current file and
  719. line-number of the source code in question.  The leap routines take
  720. this information and pass it on to the library making it able to
  721. produce verbose reports on memory not freed:
  722.  
  723.          not freed: 0x38410 ( 22 bytes) from 'malloc_t.c:92'
  724.              not freed: 0x38600 ( 10232 bytes) from 'malloc_t.c:104'
  725.    These lines from a log file shows that two allocations were not
  726. freed. One at address 0x38410 of size 22 bytes in source file
  727. `malloc_t.c' at line 92 and another at address 0x38600 of size 10232
  728. bytes at line 104 of `malloc_t.c'.
  729.  
  730.    Along with the above leap macros, `malloc.h' also contains the
  731. following macros which I have been using for all our memory allocation
  732. needs for some time now.  They take care of all the type-casting and
  733. make the code look much cleaner (IMHO).
  734.  
  735. `ALLOC(type, count)'
  736.      Usage: `long_pnt = ALLOC(long, 30);'.  This means allocate space
  737.      for 30 longs.
  738.  
  739. `MALLOC(size)'
  740.      Usage: `char_pnt = MALLOC(1000);'.  This is like ALLOC but for
  741.      characters only.  It means allocate space for 1000 characters.
  742.  
  743. `CALLOC(type, count)'
  744.      Usage: `infp = CALLOC(struct info_st, 100);'.  This means allocate
  745.      space for 100 info_st structures and zero them all.
  746.  
  747.      *NOTE*: the arguments for the CALLOC macro are sort of reversed
  748.      from calloc(unsigned int count, unsigned int size).
  749.  
  750. `REALLOC(pnt, type, count)'
  751.      Usage: `long_pnt = REALLOC(old_pnt, long, 10);'.  This takes
  752.      old_pnt and and changes its size to accommodate 10 longs.
  753.  
  754. `REMALLOC(pnt, size)'
  755.      Usage: `char_pnt = REMALLOC(char_pnt, 100);'.  This is like REALLOC
  756.      but for characters only.  It takes char_pnt and changes its size
  757.      to 100 characters.
  758.  
  759. `FREE(pnt)'
  760.      Usage: `(void)FREE(pnt);'.  This frees memory pointers.
  761.  
  762. `STRDUP(string)'
  763.      Usage: `char_pnt = STRDUP("hello");'.  This macro duplicates the
  764.      functionality of the `strdup' function.  string can be either a
  765.      static string like "hello" or a character pointer.  Non-gcc users
  766.      should use `STRDUP("hello", char_pnt);' where char_pnt is the
  767.      variable which will be assigned to the pointer to the copy of
  768.      "hello".
  769.  
  770. `BDUP(pnt, size)'
  771.      Usage: `new_item_pnt = BDUP(&item, sizeof(item));'.  This allocates
  772.      space for size bytes, copies size bytes from pnt into the new
  773.      allocation and returns it.  It is like strdup but for non-strings.
  774.       Non-gcc users should use `BDUP(&item, sizeof(item),
  775.      new_item_pnt);' where new_item_pnt is the variable which will be
  776.      assigned the pointer to the copy of item.
  777.  
  778.    In the above macro list, I have also included a STRDUP and a BDUP
  779. macro. STRDUP, for those who are not familiar with the strdup function,
  780. takes a string, allocates enough information to store the string (along
  781. with its null character), and then copies the string into the new
  782. space.  This macro does not actually call strdup but provides the same
  783. functionality and provides file and line memory information to the
  784. library.
  785.  
  786.    BDUP is a function that I invented.  I use it to duplicate
  787. structures or other elements that are not strings.  A pointer to an
  788. element and its size are passed in and the macro returns an allocated
  789. copy of it.
  790.  
  791.    gcc (GNUs c-compiler) has a neat feature in that it understands
  792. return-values from macros.  I have included a gcc form of these 2 macros
  793. (which makes them a lot more functional) as well as a non-gcc version.
  794.  
  795.    *NOTE*: I would like to strongly recommend the usage of gcc.  It is
  796. a superior compiler and future releases of this library may require its
  797. use.
  798.  
  799.  
  800. File: malloc.info,  Node: Extensions,  Next: Disabling the Library,  Prev: Allocation macros,  Up: Usage
  801.  
  802. Additional Non-Standard Routines.
  803. =================================
  804.  
  805.    The library has a number of variables and routines that are not a
  806. standard part of most malloc libraries:
  807.  
  808. `char * malloc_logpath;'
  809.      This variable can be used to set the malloc log filename.  The env
  810.      variable MALLOC_LOGFILE overrides this variable.
  811.  
  812. `int malloc_errno;'
  813.      This variable stores the internal malloc library error number like
  814.      errno does for the system calls.  It can be passed to
  815.      `malloc_strerror()' (see below) to get a string version of the
  816.      error.  It will have a value of zero if the library has not
  817.      detected any problems.
  818.  
  819. `void malloc_shutdown(void);'
  820.      This routine shuts the library down and logs the final statistics
  821.      and information especially the non-freed memory pointers.  It
  822.      should be run right before `exit()' or as the last function in
  823.      `main()'.
  824.  
  825.                   main()
  826.                   {
  827.                           ...
  828.                           malloc_shutdown();
  829.                           exit(0);
  830.                   }
  831.  
  832. `int malloc_heap_map(void);'
  833.      This routine will log to the logfile (if it is enabled) a graphical
  834.      representation of the current heap space.  It needs some work but
  835.      should provide some good information.
  836.  
  837. `int malloc_verify(char * pnt);'
  838.      Use `malloc_verify' to verify individual memory pointers that are
  839.      suspect of memory problems.  To check the entire heap pass in a
  840.      NULL or 0 pointer.
  841.  
  842.      *NOTE*: `malloc_verify' can only check the heap with the functions
  843.      that have been enabled.  For example, if fence-post checking is
  844.      not enabled in the `MALLOC_DEBUG' variable, `malloc_verify' cannot
  845.      check the fence-post areas in the heap.
  846.  
  847. `int malloc_debug(long debug);'
  848.      With this routine, the value in the `MALLOC_DEBUG' variable can be
  849.      overridden and the library debugging features set explicitly.  For
  850.      instance, if debugging should never be enabled for a program, a
  851.      call to `malloc_debug(0);' as the first call in `main()' will
  852.      disable all the memory debugging from that point on.
  853.  
  854.      One problem however is that some compilers (gcc for instance) make
  855.      calls to memory allocation functions *before* `main()' is reached
  856.      and `malloc_debug()' called meaning some debugging information may
  857.      be generated regardless.
  858.  
  859. `int malloc_examine(char * pnt, int * size, char ** file, int * line);'
  860.      This routine provides some very interesting functionality.  It
  861.      returns the size of a pnt's allocation as well as the file and
  862.      line from which it was allocated.
  863.  
  864.      *NOTE*: This function is *certainly* not portable and is not
  865.      provided by other malloc libraries.
  866.  
  867. `char * malloc_strerror(int errnum);'
  868.      `malloc_strerror' returns the string representation of the error
  869.      value in errnum (which probably should be malloc_errno).  This
  870.      allows the logging of more verbose memory error messages.
  871.  
  872.      You can also display the string representation of an error value
  873.      by a call to the `malloc_dbg' program with a `-e #' option *Note
  874.      malloc_dbg program::.
  875.  
  876.  
  877. File: malloc.info,  Node: Disabling the Library,  Prev: Extensions,  Up: Usage
  878.  
  879. How to Compile/Link Without the Library.
  880. ========================================
  881.  
  882.    When you are finished with the development and debugging sessions,
  883. you may want to disable the malloc-debug library and put in its place
  884. either the system's memory-allocation routines, gnu-malloc, or maybe
  885. your own. I have tried to make this a reasonably painless process.  The
  886. ease of the extraction depends heavily on how many of the library's
  887. features your made use of during your coding.
  888.  
  889.    I am open to any reasonable suggestions as to how to improve this
  890. process while maintaining the effectiveness of the debugging.
  891.  
  892.    * If you compiled any of your source modules with `MALLOC_FUNC_CHECK'
  893.      defined then you must first recompile all those modules without
  894.      the flag enabled.
  895.  
  896.    * If you are using any of the special functions provided by the
  897.      malloc-debug library (such as `malloc_shutdown()'), then you will
  898.      need to `#ifdef', remove, or comment them out of your code.
  899.  
  900.    * If you want to *totally* disable the malloc-debug library then you
  901.      will need to recompile all the C files that include `malloc.h'
  902.      while defining `MALLOC_DEBUG_DISABLE'.  This will cause the malloc
  903.      leap macros to not be applied *Note Allocation macros::.
  904.  
  905.                   gcc -O -g -DMALLOC_DEBUG_DISABLE main.c
  906.  
  907.    * Now you are ready to relink with a new library.  If you have not
  908.      compiled all your source with `MALLOC_DEBUG_DISABLED' defined then
  909.      you need to include the `malloc_lp.o' file on the link line.
  910.  
  911.                   gcc -O -g main.o malloc_lp.o -L/usr/local/lib -lgmalloc
  912.      If you have disabled malloc with the `MALLOC_DEBUG_DISABLED' flag
  913.      or never included `malloc.h' in any of your C files, then you will
  914.      not need to include the `malloc_lp.o' file on the link line.
  915.  
  916.                   gcc -O -g main.o -L/usr/local/lib -lgmalloc
  917.      If you get unresolved references like `_malloc_leap' or
  918.      `_malloc_bcopy' then something was not disabled as it should have
  919.      been.
  920.  
  921.  
  922. File: malloc.info,  Node: Code,  Next: Plugs,  Prev: Usage,  Up: Top
  923.  
  924. Information on the Source and General Concerns.
  925. ***********************************************
  926.  
  927. * Menu:
  928.  
  929. * Definitions::         Some terms and other information.
  930. * Compatibility::       General compatibility concerns.
  931. * Portability::         Issues important for porting the library.
  932.  
  933.  
  934. File: malloc.info,  Node: Definitions,  Next: Compatibility,  Up: Code
  935.  
  936. Some Terms and Other Information.
  937. =================================
  938.  
  939.    Here are a couple definitions and other information for those
  940. interested in "picking the brain" of the library.  The code is a little
  941. ugly here and there and it conforms to the Gray-Watson handbook of
  942. coding standards only.
  943.  
  944. "bblock"
  945.      basic block containing 2 ^ BASIC_BLOCK bytes of info
  946.  
  947. "bblock_adm"
  948.      administration for a set of basic blocks
  949.  
  950. "dblock"
  951.      divided block containing some base 2 number of blocks smaller than
  952.      a basic block.
  953.  
  954. "dblock_adm"
  955.      administration for a set of divided blocks
  956.  
  957. "chunk"
  958.      some anonymous amount of memory
  959.  
  960.  
  961. File: malloc.info,  Node: Compatibility,  Next: Portability,  Prev: Definitions,  Up: Code
  962.  
  963. General Compatibility Concerns.
  964. ===============================
  965.  
  966.    * Realloc() backwards compatibility with being able to realloc from
  967.      the last freed block is *not* supported.
  968.  
  969.    * Realloc() of a NULL pointer is supported in which case the library
  970.      will just make a call to malloc().  This is a compilation option
  971.      in the `conf.h' file.
  972.  
  973.    * The library does *not* provide memalign() nor valloc() support as
  974.      of yet, but may in future releases.  I would be interested to know
  975.      who is using these functions, which architectures they are
  976.      supported on, and for what reason they are being used.
  977.  
  978.    * Aside from possibly being slower than the system's memory
  979.      allocation functions, the library should be fully compatible with
  980.      the standard memory routines.  If this is *not* the case please
  981.      bring this to my attention.
  982.  
  983.  
  984. File: malloc.info,  Node: Portability,  Prev: Compatibility,  Up: Code
  985.  
  986. Issues Important for Porting the Library.
  987. =========================================
  988.  
  989.    General compatibility issues center around:
  990.  
  991.    * sbrk or compatible function usages
  992.  
  993.    * Whether the systems's heap grows towards high or low memory.  The
  994.      chunk.c code is designed (loosely) around the fact that consecutive
  995.      calls to sbrk should give higher memory addresses.
  996.  
  997.      I have not been able to test the library on a system whose heap
  998.      grows towards low memory.  If you are trying to run the library on
  999.      such a system I would be interested in talking with you.
  1000.  
  1001.  
  1002. File: malloc.info,  Node: Plugs,  Prev: Code,  Up: Top
  1003.  
  1004. Soapbox Comments.
  1005. *****************
  1006.  
  1007.    Since I have your attention I would like to talk for a second about a
  1008. couple of things that I feel strongly about.  If you would like any more
  1009. information about the below, please mail to the supplied addresses or
  1010. drop me a line with any questions.
  1011.  
  1012. `The Free Software Foundation <gnu@prep.ai.mit.edu>'
  1013.      As you should be able to tell by now, I am a FSF supporter.  The
  1014.      FSF's goal, as I see and support it, is to encourage the exchange
  1015.      of free source code.  The organization and its individuals have
  1016.      volunteered an amazing amount of time toward this.  If you use
  1017.      emacs, gcc, gdb, patch, perl, bison, or any of their many programs
  1018.      and libraries then you have benefited from the movement.  Please
  1019.      consider supporting it.
  1020.  
  1021. `Berkeley Software Design, Inc. <bsdi-info@bsdi.com>'
  1022.      We at the Antaire Corporation are the proud and enthusiastic
  1023.      owners of the BSD/386 operating system.  For $1k you get a
  1024.      *complete* BSD-flavor operating system with *full source* for 386
  1025.      and 486 systems (binary licenses are available).  Along with the
  1026.      obvious benefits of full source code come excellent customer
  1027.      support/service and system features such as a MS-DOG runtime
  1028.      environment, complete tcp/ip networking facilities including nfs,
  1029.      full software development utilities, X, etc.
  1030.  
  1031.  
  1032.  
  1033. Tag Table:
  1034. Node: Top1056
  1035. Node: Copying2320
  1036. Node: Allocation Basics5479
  1037. Node: Basic definitions6079
  1038. Node: Malloc functions8689
  1039. Node: Features10723
  1040. Node: Overview11324
  1041. Node: Environment variables16472
  1042. Node: malloc_dbg program19671
  1043. Node: RC file22230
  1044. Node: Debug tokens24054
  1045. Node: Argument Checking25962
  1046. Node: Usage27456
  1047. Node: Allocation macros27830
  1048. Node: Extensions31857
  1049. Node: Disabling the Library35092
  1050. Node: Code37188
  1051. Node: Definitions37547
  1052. Node: Compatibility38256
  1053. Node: Portability39208
  1054. Node: Plugs39861
  1055.  
  1056. End Tag Table
  1057.